package com.topscore.uke.ui.activities;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.topscore.uke.R;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.uke.ui.fragments.AuditFailFragment;
import com.topscore.uke.ui.fragments.PendingAuditFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class AuditActivity extends ToolBarCompatActivity {

    @Bind(R.id.tab_layout)
    CommonTabLayout tabLayout;
    @Bind(R.id.content_pager)
    ViewPager contentPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audit);
        ButterKnife.bind(this);
        setCentralTitle("入团审核");
        initViews();
    }

    private void initViews() {
        List<CustomTabEntity> tabEntityList = new ArrayList<>();
        tabEntityList.add(new CustomTabEntity() {
            @Override
            public String getTabTitle() {
                return "未审核";
            }

            @Override
            public int getTabSelectedIcon() {
                return 0;
            }

            @Override
            public int getTabUnselectedIcon() {
                return 0;
            }
        });

        tabEntityList.add(new CustomTabEntity() {
            @Override
            public String getTabTitle() {
                return "未通过";
            }

            @Override
            public int getTabSelectedIcon() {
                return 0;
            }

            @Override
            public int getTabUnselectedIcon() {
                return 0;
            }
        });

        tabLayout.setTabData((ArrayList<CustomTabEntity>) tabEntityList);
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new PendingAuditFragment());
        fragmentList.add(new AuditFailFragment());
        contentPager.setAdapter(new FragmentAdapter(fragmentList));
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                contentPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        contentPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private class FragmentAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragmentList;

        public FragmentAdapter(List<Fragment> fragments) {
            super(AuditActivity.this.getSupportFragmentManager());
            this.mFragmentList = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }
    }
}
